package com.uminho.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.uminho.egendmeprofessional.R;
import com.uminho.exceptions.InvalidOAuthParametersError;
import com.uminho.exceptions.InvalidOAuthTokenError;
import com.uminho.exceptions.OAuthServiceFailureError;
import com.uminho.http.Webservice;
import com.uminho.objects.Service;
import com.uminho.objects.Slot;

public class ServiceDay extends Activity {
	boolean overlapAppointment=false;
	EditText editTextDate = null;
	Slot slotToAppoint = null;
	long appointmentID = -1;
	Service servico = null;
	// long serviceID=-1;
	Handler messages = null;
	ArrayList<Slot> slots = null;
	GregorianCalendar day;
	ListView lister = null;
	ProgressDialog myProgressDialog = null;
	Dialog shooseTaskResource = null;
	Button ok=null;
	int EndYear=0;
	int EndMonth=0;
	int EndDay=0;
	static final int DATE_DIALOG_ID = 0;
	Long resourceID, taskID;
	EditText usernameText, commentText;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.service_day);

		ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setHomeAction(new Action() {

			public void performAction(View view) {
				ServiceDay.this.setResult(4);
				ServiceDay.this.finish();
			}

			public int getDrawable() {
				return R.drawable.icon1;
			}
		});
		actionBar.setTitle(R.string.service_slots);

		initHandler();

		// set service id
		Bundle b = getIntent().getExtras();
		long syear = b.getLong("servicoyear", -1);
		long smonth = b.getLong("servicomonth", -1);
		long sday = b.getLong("servicoday", -1);
		resourceID=b.getLong("resourceid", -1);
		taskID=b.getLong("taskid", -1);;
		servico = (Service) getIntent().getExtras().get("service");
		day = new GregorianCalendar();
		day.set(Calendar.DAY_OF_MONTH, (int) sday);
		day.set(Calendar.MONTH, (int) smonth);
		day.set(Calendar.YEAR, (int) syear);

		if (day == null) {
			day = new GregorianCalendar();
		}
		getSlots();
	}

	private void initHandler() {
		messages = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				Log.d("Service Day", "what " + msg.what);
				if (msg.what == -1) {
					Toast.makeText(ServiceDay.this,
							getString(R.string.impossiblereachtheserver), 3000)
							.show();
				} else if (msg.what == -2) {
					Toast.makeText(ServiceDay.this,
							getString(R.string.impossible_assign), 3000).show();
				} else if (msg.what == -3) {
					//empty field
					Toast.makeText(ServiceDay.this,
							getString(R.string.emptyfields), 3000).show();
				
				}else if (msg.what == -4) {
					/*
					 * Overlap
					 */
					AlertDialog.Builder builder = new AlertDialog.Builder(ServiceDay.this);
					builder.setTitle(R.string.overlapproblem);
					builder.setMessage(getString(R.string.overlapquestion))
					       .setCancelable(false)
					       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					              overlapAppointment=true;
					              //messages.sendEmptyMessage(30);
					              addAppointment();
					             
					           }
					       })
					       .setNegativeButton("No", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					AlertDialog alert = builder.create();
					builder.show();
				}/*else if (msg.what == -5) {
					
					 // force validate creation
					 
					AlertDialog.Builder builder = new AlertDialog.Builder(ServiceDay.this);
					builder.setTitle(R.string.forcevalidateproblem);
					builder.setMessage(getString(R.string.forcevalidatequestion))
					       .setCancelable(false)
					       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					              overlapAppointment=true;
					              forcevalidcreation=true;
					              //addAppointment();
					             
					           }
					       })
					       .setNegativeButton("No", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					AlertDialog alert = builder.create();
					builder.show();
				}*/
				else if(msg.what==30){
					Dialog confirmAppointment = new Dialog(ServiceDay.this);
					confirmAppointment.setContentView(R.layout.provideraddappointmentdialog);
					confirmAppointment.setTitle(R.string.addapointmenttitle);
					usernameText =  (EditText) confirmAppointment.findViewById(R.id.editTextUsername);
					commentText = (EditText) confirmAppointment.findViewById(R.id.editTextComment);
					Button okButton = (Button) confirmAppointment.findViewById(R.id.buttonOkAdd);
					okButton.setOnClickListener(new OnClickListener() {
						public void onClick(View v) {
							if(usernameText.getText().length()>0)
					        messages.sendEmptyMessage(20);
					         else  messages.sendEmptyMessage(-3);
						}
					});
					
				}
				else if (msg.what == 20) {
					Toast.makeText(ServiceDay.this, "Adicionar appointment", 3000).show();
					/**
					 * adicionar os appointments aqui
					 */
					addAppointment();
				} else if (msg.what == 40) {
					// add appointment success!
					Toast.makeText(
							ServiceDay.this,
							ServiceDay.this
									.getString(R.string.successassignappointment),
							3000).show();
					ServiceDay.this.setResult(5, ServiceDay.this.getIntent());
					ServiceDay.this.finish();

				} else {
					// connection error

				}

				if (slots == null || slots.size() <= 0) {
					// Sem slots
					Toast.makeText(ServiceDay.this,
							getString(R.string.connection_error), 3000);
					slots = new ArrayList<Slot>();
				}
				organizeAppointments(slots);
				initListView();
				if (myProgressDialog.isShowing())
					myProgressDialog.dismiss();
			}
		};
	}
	private void addAppointment(){
		Log.d("Add Appointment", "init date: "+slotToAppoint.getStartDate().getTime().toString());
		Log.d("Add Appointment", "end date: "+slotToAppoint.getEndDate().getTime().toString());
		Log.d("Add Appointment", "Service id: "+servico.getId()+" name: "+servico.getName());
		Log.d("Add Appointment", "Resource id: "+resourceID);
		Log.d("Add Appointment", "Task id: "+taskID);
		Log.d("Add Appointment", "Username: "+usernameText.getText().toString());
		Log.d("Add Appointment", "Comment: "+commentText.getText().toString());
		
		myProgressDialog = ProgressDialog.show(
				ServiceDay.this,
				getString(R.string.loading),
				getString(R.string.addappointment), true);

		Thread t = new Thread() {
			public void run() {

				Webservice w = Webservice.getInstance();
				try {
					
				/*} catch (InvalidOAuthTokenError e) {
					messages.sendEmptyMessage(-1);
					Log.d("Add Appointment", "token error");
					return;
				} catch (InvalidOAuthParametersError e) {
					messages.sendEmptyMessage(-1);
					Log.d("Add Appointment",
							"autentication");
					return;
				}catch (userOverlappedAppointmentsException oe) {
					messages.sendEmptyMessage(-4);
					Log.d("Add Appointment","overlap?");
					return;
				}catch (RecurrentAppointmentsException fve) {
				messages.sendEmptyMessage(-5);
				Log.d("Add Appointment","forcevalidcreation?");
				return;
			
				*/}catch (Exception e) {
					Log.d("Excepção", "" + e.toString());
					e.printStackTrace();
					messages.sendEmptyMessage(-2);
					overlapAppointment=false;
					return;
				}
				messages.sendEmptyMessage(40);
				overlapAppointment=false;
			}
		};
		t.start();
		
	}
	private void getSlots() {
		myProgressDialog = ProgressDialog.show(this,
				getString(R.string.loading), getString(R.string.getDayView),
				true);
		new Thread() {
			@Override
			public void run() {

				try {
					Webservice w = Webservice.getInstance();
					Log.d("getslot", "sid: " + servico.getId() + " day "
							+ day.getTime().toString());
					slots = w.getSlotByServiceByDay(servico.getId(), day);

				} catch (InvalidOAuthParametersError e) {
					messages.sendEmptyMessage(-1);
					return;
				} catch (InvalidOAuthTokenError e) {
					messages.sendEmptyMessage(-1);
					return;
				} catch (OAuthServiceFailureError e) {
					messages.sendEmptyMessage(-1);
					return;
				} catch (Exception e) {
					e.printStackTrace();
					if (slots == null || slots.size() <= 0) {
						messages.sendEmptyMessage(-2);
						return;
					}
				}
				Log.d("getslot nr: ", "" + slots.size());
				messages.sendEmptyMessage(1);

			}
		}.start();
	}

	/*private void initAdvanced() {
		
		// button advanced
		
		final CheckBox advanced = (CheckBox) shooseTaskResource
				.findViewById(R.id.checkBoxAdvancedMode);
		final LinearLayout advancedLayout = (LinearLayout) shooseTaskResource
				.findViewById(R.id.linearLayoutAdvanced);

		advanced.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (advanced.isChecked()) {
					advancedLayout.setVisibility(LinearLayout.VISIBLE);
				} else {
					advancedLayout.setVisibility(LinearLayout.GONE);
				}
			}
		});
		
		 // advanced components
		 
		final CheckBox chooseDate = (CheckBox) shooseTaskResource
				.findViewById(R.id.checkBoxChooseDate);
		final LinearLayout timesLayout = (LinearLayout) shooseTaskResource
				.findViewById(R.id.linearLayoutChooseTimes);
		final LinearLayout dateLayout = (LinearLayout) shooseTaskResource
				.findViewById(R.id.linearLayoutChooseDate);

		chooseDate.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (chooseDate.isChecked()) {
					timesLayout.setVisibility(LinearLayout.GONE);
					dateLayout.setVisibility(LinearLayout.VISIBLE);
				} else {
					timesLayout.setVisibility(LinearLayout.VISIBLE);
					dateLayout.setVisibility(LinearLayout.GONE);
				}
			}
		});
		
		 // date textbox Ao clicar na data mostra o datepiker
		 
		editTextDate = (EditText) shooseTaskResource
				.findViewById(R.id.editTextDate);
		editTextDate.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});
	}

	*/
	private void organizeAppointments(ArrayList<Slot> sl) {

		ArrayList<Slot> aux = sl;

		Collections.sort(aux, new Comparator() {
			public int compare(Object o1, Object o2) {
				Slot p1 = (Slot) o1;
				Slot p2 = (Slot) o2;
				return p1.getStartDate().getTime()
						.compareTo(p2.getStartDate().getTime());
			}
		});
		slots = aux;

	}

	private void initListView() {
		lister = (ListView) findViewById(R.id.list);
		ItemsAdapter itemsAdapter = new ItemsAdapter(ServiceDay.this);
		lister.setAdapter(itemsAdapter);
		
	}

	private class ItemsAdapter extends BaseAdapter {

		SimpleDateFormat dateFormat;

		public ItemsAdapter(Context context) {
			dateFormat = new SimpleDateFormat("HH'h'mm'm'");
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			TextView mDescription;
			TextView mTitle;
			Button image;
			View view = convertView;

			if (view == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.service_row, null);
			}
			mTitle = (TextView) view.findViewById(R.id.service_row_title);
			mTitle.setText(dateFormat.format(slots.get(position).getStartDate()
					.getTime()));

			mDescription = (TextView) view
					.findViewById(R.id.service_row_description);
			/*
			 * mDescription.setText(dateFormat.format(slots.get(position).getEndDate
			 * ().getTime()));
			 */
			final Slot s = slots.get(position);
			Long appointId = s.getAppointmentId();
			/*Log.d("slot desc", "id " + appointId + " begin date: "
					+ s.getStartDate().getTime().toString() + " end date: "
					+ s.getEndDate().getTime().toString());*/
			image = (Button) view.findViewById(R.id.image);
			if (appointId == null || appointId == -1) {
				// indisponivel
				image.setCompoundDrawables(
						getResources().getDrawable(R.drawable.unavailable),
						null, null, null);
				image.setText(getResources().getString(R.string.unavailable));
				image.setEnabled(false);

				mDescription.setText(getResources().getString(
						R.string.unavailable));
				mDescription.setTextColor(Color.rgb(255, 51, 51));
			} else if (appointId == 0) {
				// livre
				image.setCompoundDrawablesWithIntrinsicBounds(getResources()
						.getDrawable(R.drawable.available), null, null, null);
				image.setText(getResources().getString(R.string.assign));

				mDescription.setText(getResources().getString(
						R.string.available));
				mDescription.setTextColor(Color.rgb(51, 153, 51));
			} else {
				// ocupado por cliente
				image.setCompoundDrawablesWithIntrinsicBounds(getResources()
						.getDrawable(R.drawable.exchange), null, null, null);
				image.setText(getResources().getString(R.string.exchange));

				mDescription.setText(getResources().getString(R.string.busy));
				mDescription.setTextColor(Color.rgb(255, 153, 51));
			}
			image.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					if (s.getAppointmentId() == 0) {

						slotToAppoint = slots.get(position);
						appointmentID = slotToAppoint.getAppointmentId();
						messages.sendEmptyMessage(30);
					} else if (s.getAppointmentId() == null
							|| s.getAppointmentId() == -1)
						messages.sendEmptyMessage(-2);
				}
			});
			return view;
		}

		public int getCount() {
			return slots.size();
		}

		public Object getItem(int position) {
			return slots.get(position);
		}

		public long getItemId(int position) {
			if (slots.get(position).getAppointmentId() != null)
				return slots.get(position).getAppointmentId();
			// else return -1;
			else
				return -1;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.d("ServiceDay result", "req " + requestCode + " res " + resultCode);
	}

	/*@Override
	protected Dialog onCreateDialog(int id) {
		Calendar calendario = Calendar.getInstance();
		int ano = day.get(GregorianCalendar.YEAR);
		int mes = day.get(GregorianCalendar.MONTH);
		int dia = day.get(GregorianCalendar.DAY_OF_MONTH);

		switch (id) {
		case DATE_DIALOG_ID:
			Log.d("date", day.getTime().toGMTString());
			DatePickerDialog d = new DatePickerDialog(this, mDateSetListener,
					ano, mes, dia);

			return d;
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			editTextDate.setText(dayOfMonth + "-" + monthOfYear + "-" + year);
			EndYear = year;
			EndMonth = monthOfYear;
			EndDay = dayOfMonth;
		}
	};*/
}
